/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.service.admin.AdminDocumentHandler;
/*     */ import com.zimbra.cs.service.admin.LockoutMailbox;
/*     */ import com.zimbra.soap.admin.message.GrantRightRequest;
/*     */ import com.zimbra.soap.admin.message.GrantRightResponse;
/*     */ import com.zimbra.soap.admin.message.LockoutMailboxRequest;
/*     */ import com.zimbra.soap.admin.message.LockoutMailboxResponse;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import com.zimbra.soap.admin.type.EffectiveRightsTargetSelector;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.admin.type.RightModifierInfo;
/*     */ import com.zimbra.soap.type.AccountNameSelector;
/*     */ import com.zimbra.soap.type.GranteeType;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.After;
/*     */ import org.junit.Before;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLockoutMailbox
/*     */   extends TestCase
/*     */ {
/*     */   private static final String MY_DOMAIN = "TestLockoutMailbox-mydomain.com";
/*     */   private static final String OFFLIMITS_DOMAIN = "offlimits.com";
/*     */   private static final String DELEGATED_ADMIN_NAME = "delegated-admin@TestLockoutMailbox-mydomain.com";
/*     */   private static final String MY_USER = "user1@TestLockoutMailbox-mydomain.com";
/*     */   private static final String MY_NON_EXISTING_USER = "user2@TestLockoutMailbox-mydomain.com";
/*     */   private static final String OFFLIMITS_NON_EXISTING_USER = "user2@offlimits.com";
/*     */   private static final String OFFLIMITS_USER1 = "user1@offlimits.com";
/*  51 */   private Account domainAdmin = null;
/*  52 */   private SoapProvisioning adminSoapProv = null;
/*  53 */   private SoapProvisioning delegatedSoapProv = null;
/*     */   
/*     */   @Before
/*     */   public void setUp() throws Exception {
/*  57 */     cleanup();
/*  58 */     this.adminSoapProv = TestUtil.newSoapProvisioning();
/*  59 */     TestJaxbProvisioning.ensureDomainExists("TestLockoutMailbox-mydomain.com");
/*  60 */     TestJaxbProvisioning.ensureDomainExists("offlimits.com");
/*  61 */     this.adminSoapProv.createAccount("user1@TestLockoutMailbox-mydomain.com", "test123", null);
/*  62 */     this.adminSoapProv.createAccount("user1@offlimits.com", "test123", null);
/*     */   }
/*     */   
/*     */   @After
/*     */   public void tearDown() throws Exception {
/*  67 */     cleanup();
/*     */   }
/*     */   
/*     */   private void cleanup() throws Exception {
/*  71 */     Account acct = TestUtil.getAccount("user1@TestLockoutMailbox-mydomain.com");
/*  72 */     if (acct != null) {
/*  73 */       if (MailboxManager.getInstance().isMailboxLockedOut(acct.getId())) {
/*  74 */         MailboxManager.getInstance().undoLockout(acct.getId());
/*     */       }
/*  76 */       TestUtil.deleteAccount("user1@TestLockoutMailbox-mydomain.com");
/*     */     }
/*  78 */     if (this.domainAdmin != null) {
/*  79 */       this.domainAdmin.deleteAccount();
/*     */     }
/*  81 */     TestJaxbProvisioning.deleteAccountIfExists("user1@offlimits.com");
/*  82 */     TestJaxbProvisioning.deleteDomainIfExists("TestLockoutMailbox-mydomain.com");
/*  83 */     TestJaxbProvisioning.deleteDomainIfExists("offlimits.com");
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testLockout() throws Exception {
/*  88 */     Mailbox mbox = TestUtil.getMailbox("user1@TestLockoutMailbox-mydomain.com");
/*  89 */     TestUtil.addMessage(mbox, "test");
/*  90 */     TestUtil.waitForMessage(TestUtil.getZMailbox("user1@TestLockoutMailbox-mydomain.com"), "test");
/*  91 */     assertFalse("mailbox should not be locked yet", MailboxManager.getInstance().isMailboxLockedOut(mbox.getAccountId()));
/*     */     
/*  93 */     LockoutMailboxRequest req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user1@TestLockoutMailbox-mydomain.com"));
/*  94 */     req.setOperation("start");
/*  95 */     LockoutMailboxResponse resp = (LockoutMailboxResponse)this.adminSoapProv.invokeJaxb(req);
/*  96 */     assertNotNull("LockoutMailboxRequest return null response", resp);
/*  97 */     assertTrue("mailbox should be locked now", MailboxManager.getInstance().isMailboxLockedOut(mbox.getAccountId()));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testUnlock() throws Exception {
/* 102 */     Mailbox mbox = TestUtil.getMailbox("user1@TestLockoutMailbox-mydomain.com");
/* 103 */     TestUtil.addMessage(mbox, "test");
/* 104 */     TestUtil.waitForMessage(TestUtil.getZMailbox("user1@TestLockoutMailbox-mydomain.com"), "test");
/* 105 */     assertFalse("mailbox should not be locked yet", MailboxManager.getInstance().isMailboxLockedOut(mbox.getAccountId()));
/*     */     
/* 107 */     MailboxManager.getInstance().lockoutMailbox(mbox.getAccountId());
/* 108 */     assertTrue("mailbox should be locked now", MailboxManager.getInstance().isMailboxLockedOut(mbox.getAccountId()));
/*     */     
/* 110 */     LockoutMailboxRequest req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user1@TestLockoutMailbox-mydomain.com"));
/* 111 */     req.setOperation("end");
/* 112 */     LockoutMailboxResponse resp = (LockoutMailboxResponse)this.adminSoapProv.invokeJaxb(req);
/* 113 */     assertNotNull("LockoutMailboxRequest return null response", resp);
/* 114 */     assertFalse("mailbox should not be locked any more", MailboxManager.getInstance().isMailboxLockedOut(mbox.getAccountId()));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testLockAccountEnumeration() throws Exception
/*     */   {
/* 120 */     Mailbox mbox = TestUtil.getMailbox("user1@TestLockoutMailbox-mydomain.com");
/* 121 */     TestUtil.addMessage(mbox, "test");
/* 122 */     TestUtil.waitForMessage(TestUtil.getZMailbox("user1@TestLockoutMailbox-mydomain.com"), "test");
/* 123 */     List<AdminRight> relatedRights = new ArrayList();
/* 124 */     List<String> notes = new ArrayList();
/* 125 */     AdminDocumentHandler handler = new LockoutMailbox();
/* 126 */     handler.docRights(relatedRights, notes);
/* 127 */     createDelegatedAdmin(relatedRights);
/* 128 */     LockoutMailboxRequest req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user2@offlimits.com"));
/* 129 */     req.setOperation("start");
/*     */     try {
/* 131 */       this.delegatedSoapProv.invokeJaxb(req);
/* 132 */       fail("should have caught an exception");
/*     */     } catch (SoapFaultException e) {
/* 134 */       assertEquals("should be getting 'Permission Denied' response", "service.PERM_DENIED", e.getCode());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testLockoutSufficientPermissions() throws Exception {
/* 140 */     Mailbox mbox = TestUtil.getMailbox("user1@TestLockoutMailbox-mydomain.com");
/* 141 */     TestUtil.addMessage(mbox, "test");
/* 142 */     TestUtil.waitForMessage(TestUtil.getZMailbox("user1@TestLockoutMailbox-mydomain.com"), "test");
/* 143 */     List<AdminRight> relatedRights = new ArrayList();
/* 144 */     List<String> notes = new ArrayList();
/* 145 */     AdminDocumentHandler handler = new LockoutMailbox();
/* 146 */     handler.docRights(relatedRights, notes);
/* 147 */     createDelegatedAdmin(relatedRights);
/* 148 */     LockoutMailboxRequest req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user1@TestLockoutMailbox-mydomain.com"));
/* 149 */     req.setOperation("start");
/*     */     try {
/* 151 */       LockoutMailboxResponse resp = (LockoutMailboxResponse)this.delegatedSoapProv.invokeJaxb(req);
/* 152 */       assertNotNull("LockoutMailboxResponse should not be null", resp);
/*     */     } catch (SoapFaultException e) {
/* 154 */       fail("should not be getting an exception");
/*     */     }
/*     */     
/* 157 */     req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user2@TestLockoutMailbox-mydomain.com"));
/* 158 */     req.setOperation("start");
/*     */     try {
/* 160 */       this.delegatedSoapProv.invokeJaxb(req);
/* 161 */       fail("should have caught an exception");
/*     */     } catch (SoapFaultException e) {
/* 163 */       assertEquals("should be getting 'no such account' response", "account.NO_SUCH_ACCOUNT", e.getCode());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testLockoutAsGlobalAdmin() throws Exception
/*     */   {
/* 170 */     Mailbox mbox = TestUtil.getMailbox("user1@TestLockoutMailbox-mydomain.com");
/* 171 */     TestUtil.addMessage(mbox, "test");
/* 172 */     TestUtil.waitForMessage(TestUtil.getZMailbox("user1@TestLockoutMailbox-mydomain.com"), "test");
/* 173 */     LockoutMailboxRequest req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user1@TestLockoutMailbox-mydomain.com"));
/* 174 */     req.setOperation("start");
/*     */     try {
/* 176 */       LockoutMailboxResponse resp = (LockoutMailboxResponse)this.adminSoapProv.invokeJaxb(req);
/* 177 */       assertNotNull("LockoutMailboxResponse should not be null", resp);
/*     */     } catch (SoapFaultException e) {
/* 179 */       fail("should not be getting an exception");
/*     */     }
/*     */     
/* 182 */     req = LockoutMailboxRequest.create(AccountNameSelector.fromName("user2@offlimits.com"));
/* 183 */     req.setOperation("start");
/*     */     try {
/* 185 */       this.adminSoapProv.invokeJaxb(req);
/* 186 */       fail("should have caught an exception");
/*     */     } catch (SoapFaultException e) {
/* 188 */       assertEquals("should be getting 'no such account' response", "account.NO_SUCH_ACCOUNT", e.getCode());
/*     */     }
/*     */   }
/*     */   
/*     */   public void createDelegatedAdmin(List<AdminRight> relatedRights) throws ServiceException
/*     */   {
/* 194 */     Map<String, Object> attrs = new HashMap();
/* 195 */     StringUtil.addToMultiMap(attrs, "zimbraIsDelegatedAdminAccount", "TRUE");
/* 196 */     this.domainAdmin = this.adminSoapProv.createAccount("delegated-admin@TestLockoutMailbox-mydomain.com", "test123", attrs);
/* 197 */     assertNotNull("failed to create domin admin account", this.domainAdmin);
/* 198 */     for (AdminRight r : relatedRights) {
/* 199 */       String target = null;
/* 200 */       com.zimbra.cs.account.accesscontrol.TargetType targetType = null;
/* 201 */       if (r.getTargetType() == com.zimbra.cs.account.accesscontrol.TargetType.domain) {
/* 202 */         targetType = com.zimbra.cs.account.accesscontrol.TargetType.domain;
/* 203 */         target = "TestLockoutMailbox-mydomain.com";
/* 204 */       } else if ((r.getTargetType() == com.zimbra.cs.account.accesscontrol.TargetType.account) || (r.getTargetType() == com.zimbra.cs.account.accesscontrol.TargetType.calresource))
/*     */       {
/* 206 */         targetType = com.zimbra.cs.account.accesscontrol.TargetType.domain;
/* 207 */         target = "TestLockoutMailbox-mydomain.com";
/* 208 */       } else if (r.getTargetType() == com.zimbra.cs.account.accesscontrol.TargetType.server) {
/* 209 */         targetType = com.zimbra.cs.account.accesscontrol.TargetType.server;
/* 210 */         target = Provisioning.getInstance().getLocalServer().getName();
/*     */       }
/* 212 */       grantRightToAdmin(this.adminSoapProv, com.zimbra.soap.type.TargetType.fromString(targetType.toString()), target, "delegated-admin@TestLockoutMailbox-mydomain.com", r.getName());
/*     */     }
/*     */     
/* 215 */     this.adminSoapProv.flushCache(CacheEntryType.acl, null);
/* 216 */     this.delegatedSoapProv = TestUtil.newDelegatedSoapProvisioning("delegated-admin@TestLockoutMailbox-mydomain.com", "test123");
/*     */   }
/*     */   
/*     */   private static void grantRightToAdmin(SoapProvisioning adminSoapProv, com.zimbra.soap.type.TargetType targetType, String targetName, String granteeName, String rightName) throws ServiceException
/*     */   {
/* 221 */     GranteeSelector grantee = new GranteeSelector(GranteeType.usr, GranteeSelector.GranteeBy.name, granteeName);
/* 222 */     EffectiveRightsTargetSelector target = null;
/* 223 */     if (targetName == null) {
/* 224 */       target = new EffectiveRightsTargetSelector(targetType, null, null);
/*     */     } else {
/* 226 */       target = new EffectiveRightsTargetSelector(targetType, TargetBy.name, targetName);
/*     */     }
/*     */     
/* 229 */     RightModifierInfo right = new RightModifierInfo(rightName);
/* 230 */     GrantRightResponse grResp = (GrantRightResponse)adminSoapProv.invokeJaxb(new GrantRightRequest(target, grantee, right));
/* 231 */     assertNotNull("GrantRightResponse for " + right.getValue(), grResp);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestLockoutMailbox.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */